package org.lg.service.impl;

import org.lg.config.Resource;
import org.lg.config.Service;
import org.lg.dao.ClazzDao;
import org.lg.dao.ScDao;
import org.lg.dao.StudentDao;
import org.lg.dao.impl.StudentDaoImpl;
import org.lg.dto.BaseDto;
import org.lg.dto.MessageEnum;
import org.lg.entity.Sc;
import org.lg.entity.Student;
import org.lg.page.PageInfo;
import org.lg.service.StudentService;
import org.lg.vo.Vo;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;

//业务逻辑层 分担了控制器层 controller 与  数据访问层 dao的业务逻辑部分的判断与计算，例如下单失败是因为库存不足
@Service("studentService")//这个名字和controller需要的service的名字对应
public class StudentServiceImpl implements StudentService {
    @Resource
    StudentDao studentDao;
    @Resource
    ScDao scDao;

    @Override

    public BaseDto getAllStudents() throws Exception {
        List<Student> students = studentDao.getAllStudents();
        Optional<List<Student>> op = Optional.ofNullable(students) ;
        MessageEnum messageEnum = op.isPresent()?MessageEnum.GETALLSTUDENTSUCCESS:MessageEnum.GETSTUDENTBYIDFAIL;//如果成立 赋值 赋？与：之间 否则：之后
        BaseDto dto = new BaseDto(messageEnum,op.get());//如果没有查到数据 前端拿到的时候失败的枚举和null的集合，如果成功前端拿到的是成功的枚举 和  查询到的集合

        return dto;
    }

    @Override

    public BaseDto getTop20Student() throws Exception {
        List<Student> students = studentDao.getTop20Student();
        Optional<List<Student>> op = Optional.ofNullable(students) ;
        MessageEnum messageEnum = op.isPresent()?MessageEnum.GETALLSTUDENTSUCCESS:MessageEnum.GETSTUDENTBYIDFAIL;//如果成立 赋值 赋？与：之间 否则：之后
        BaseDto dto = new BaseDto(messageEnum,op.get());//如果没有查到数据 前端拿到的时候失败的枚举和null的集合，如果成功前端拿到的是成功的枚举 和  查询到的集合

        return dto;
    }

    public BaseDto getScoreBySno(String sno) throws Exception {
        List<Sc> scs = scDao.getScBySno(sno);
        Optional<List<Sc>> op = Optional.ofNullable(scs) ;
        MessageEnum messageEnum = op.isPresent()?MessageEnum.GETALLSTUDENTSUCCESS:MessageEnum.GETSTUDENTBYIDFAIL;//如果成立 赋值 赋？与：之间 否则：之后
        BaseDto dto = new BaseDto(messageEnum,op.get());//如果没有查到数据 前端拿到的时候失败的枚举和null的集合，如果成功前端拿到的是成功的枚举 和  查询到的集合

        return dto;
    }





    /**
     * 分页查询学生
     * @param pageInfo
     * @return
     */

    public BaseDto getStudentByPage(PageInfo pageInfo) throws Exception {
        List<Student> students = studentDao.getStudentByPage(pageInfo);
        Optional<List<Student>> op = Optional.ofNullable(students) ;
        MessageEnum messageEnum = op.isPresent()?MessageEnum.GETALLSTUDENTSUCCESS:MessageEnum.GETSTUDENTBYIDFAIL;//如果成立 赋值 赋？与：之间 否则：之后
        BaseDto dto = new BaseDto(messageEnum,op.get());//如果没有查到数据 前端拿到的时候失败的枚举和null的集合，如果成功前端拿到的是成功的枚举 和  查询到的集合
        dto.setObj(pageInfo);
        return dto;
    }

    @Override
    public BaseDto getStudentById(String sno) throws Exception {
        Student student = studentDao.getStudentBySno(sno);
        Optional<Student> op = Optional.ofNullable(student) ;
        MessageEnum messageEnum = op.isPresent()?MessageEnum.GETALLSTUDENTSUCCESS:MessageEnum.GETSTUDENTBYIDFAIL;//如果成立 赋值 赋？与：之间 否则：之后
        BaseDto dto = new BaseDto(messageEnum,op.get());
        return dto;
    }

    /**
     * 根据主键查询的学生关联班级和班主任信息
     * @param sno
     * @return
     * @throws Exception
     */
    @Override
    public BaseDto selectByPrimaryKeyWithClazzAndTeacher(String sno)  throws Exception {
        Student student = studentDao.selectByPrimaryKeyWithClazzAndTeacher(sno);
        Optional<Student> op = Optional.ofNullable(student) ;
        MessageEnum messageEnum = op.isPresent()?MessageEnum.GETALLSTUDENTSUCCESS:MessageEnum.GETSTUDENTBYIDFAIL;//如果成立 赋值 赋？与：之间 否则：之后
        BaseDto dto = new BaseDto(messageEnum,op.get());
        return dto;
    }

    @Override
    public BaseDto addStudent(Student student) throws Exception {
        Integer result = studentDao.addStudent(student);
        MessageEnum messageEnum = result>0?MessageEnum.ADDSTUDENTSUCCESS:MessageEnum.GETSTUDENTBYIDFAIL;//如果成立 赋值 赋？与：之间 否则：之后
        BaseDto dto = new BaseDto(messageEnum);
        return dto;
    }
    @Override
    public BaseDto editStudent(Student student) throws Exception {
        Integer result = studentDao.editStudent(student);
        MessageEnum messageEnum = result>0?MessageEnum.ADDSTUDENTSUCCESS:MessageEnum.GETSTUDENTBYIDFAIL;//如果成立 赋值 赋？与：之间 否则：之后
        BaseDto dto = new BaseDto(messageEnum);
        return dto;
    }

    @Override
    public BaseDto getScBySnoAndByPage(PageInfo pageInfo) throws Exception {
        List<Sc> scs = scDao.getScBySnoAndByPage(pageInfo);
        Optional<List<Sc>> op = Optional.ofNullable(scs) ;
        MessageEnum messageEnum = op.isPresent()?MessageEnum.GETALLSTUDENTSUCCESS:MessageEnum.GETSTUDENTBYIDFAIL;//如果成立 赋值 赋？与：之间 否则：之后
        BaseDto dto = new BaseDto(messageEnum,op.get());//如果没有查到数据 前端拿到的时候失败的枚举和null的集合，如果成功前端拿到的是成功的枚举 和  查询到的集合
       dto.setObj(pageInfo);
        return dto;
    }

    @Override
    public BaseDto getClassMateByCno(Vo vo) throws Exception {
        String cno = vo.getValues().get("cno");
        String sno = vo.getValues().get("sno");
        List<Student> students = studentDao.getStudentByCno(cno);
        students = students.stream().filter(stu-> !stu.getSno().equals(sno)).collect(Collectors.toList());
        Optional<List<Student>> op = Optional.ofNullable(students) ;
        MessageEnum messageEnum = op.isPresent()?MessageEnum.GETALLSTUDENTSUCCESS:MessageEnum.GETSTUDENTBYIDFAIL;//如果成立 赋值 赋？与：之间 否则：之后
        BaseDto dto = new BaseDto(messageEnum,op.get());//如果没有查到数据 前端拿到的时候失败的枚举和null的集合，如果成功前端拿到的是成功的枚举 和  查询到的集合
        dto.settList(students);
        return dto;
    }
    @Override
    public BaseDto getClassMateByCnoAndByPage(PageInfo pageInfo) throws Exception {

        List<Student> students = studentDao.getClassMateByCnoAndByPage(pageInfo);
        String sno = pageInfo.getConditions().get("sno");
//        students = students.stream().filter(stu-> !stu.getSno().equals(sno)).collect(Collectors.toList()); //排除自己
        Optional<List<Student>> op = Optional.ofNullable(students) ;
        MessageEnum messageEnum = op.isPresent()?MessageEnum.GETALLSTUDENTSUCCESS:MessageEnum.GETSTUDENTBYIDFAIL;//如果成立 赋值 赋？与：之间 否则：之后
        BaseDto dto = new BaseDto(messageEnum,op.get());//如果没有查到数据 前端拿到的时候失败的枚举和null的集合，如果成功前端拿到的是成功的枚举 和  查询到的集合
        dto.settList(students);
        dto.setObj(pageInfo);
        return dto;
    }


}
